package bento.views.pnls;

import java.awt.Component;

import javax.swing.JPanel;

import bento.IHasWrapper;
import bento.IWrapper;
import bento.api.IBentoClient.IStoreManager;
import bento.api.IQuerier;
import bento.types.IStore;
import bento.utils.IPredicate;
import bento.utils.Predicate;
import bento.views.pnls.PnlStores.Wrapper;

import javax.swing.JTabbedPane;

import java.awt.BorderLayout;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JSplitPane;
import javax.swing.border.TitledBorder;
import javax.swing.JScrollPane;
import javax.swing.Box;

import java.awt.GridLayout;

import javax.swing.JCheckBox;
import javax.swing.border.EtchedBorder;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JTextPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;

import bento.layout.WrapLayout;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class PnlStores extends JPanel implements IHasWrapper<PnlStores.Wrapper> {
    private Wrapper wrapper;
    private JTextField txtName;
    private JTextField txtTel;
    private JTable table;
    private JButton btnAppend;
    private JButton btnRemove;
    private JButton btnCommit;
    private JButton btnRefresh;
    private JCheckBox chkName;
    private JCheckBox chkTel;
    private JTabbedPane tabbedPane;
    private JPanel panel_5;
    private PnlBentos pnlBentos;
    
    
    @Override
    public Wrapper getWrapper() {
        return wrapper;
    }

    /**
     * Create the panel.
     */
    public PnlStores() {
        setLayout(new BorderLayout(0, 0));
        
        tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
        add(tabbedPane, BorderLayout.CENTER);
        
        panel_5 = new JPanel();
        tabbedPane.addTab("Stores", null, panel_5, null);
        panel_5.setLayout(new BorderLayout(0, 0));
        
        JPanel panel = new JPanel();
        panel_5.add(panel, BorderLayout.EAST);
        panel.setBorder(new TitledBorder(null, "Filter", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        panel.setLayout(new BorderLayout(0, 0));
        
        JPanel panel_4 = new JPanel();
        panel.add(panel_4, BorderLayout.NORTH);
        panel_4.setLayout(new BoxLayout(panel_4, BoxLayout.Y_AXIS));
        
        JPanel panel_2 = new JPanel();
        panel_4.add(panel_2);
        panel_2.setBorder(new TitledBorder(null, "Name", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        
        chkName = new JCheckBox("");
        panel_2.add(chkName);
        
        txtName = new JTextField();
        panel_2.add(txtName);
        txtName.setColumns(10);
        
        JPanel panel_3 = new JPanel();
        panel_4.add(panel_3);
        panel_3.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Tel", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        
        chkTel = new JCheckBox("");
        panel_3.add(chkTel);
        
        txtTel = new JTextField();
        txtTel.setColumns(10);
        panel_3.add(txtTel);
        
        JPanel panel_1 = new JPanel();
        panel_5.add(panel_1, BorderLayout.CENTER);
        panel_1.setBorder(new TitledBorder(null, "Stores", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        panel_1.setLayout(new BorderLayout(0, 0));
        
        JPanel panel_7 = new JPanel();
        panel_1.add(panel_7, BorderLayout.CENTER);
        panel_7.setLayout(new BorderLayout(0, 0));
        
        JScrollPane scrollPane = new JScrollPane();
        panel_7.add(scrollPane);
        
        table = new JTable();
        table.setModel(new DefaultTableModel(
            new Object[][] {
            },
            new String[] {
                "Name", "Tel", "Desc"
            }
        ));
        scrollPane.setViewportView(table);
        
        JPanel panel_8 = new JPanel();
        panel_7.add(panel_8, BorderLayout.EAST);
        panel_8.setLayout(new FormLayout(new ColumnSpec[] {
                FormFactory.DEFAULT_COLSPEC,},
            new RowSpec[] {
                FormFactory.DEFAULT_ROWSPEC,
                FormFactory.DEFAULT_ROWSPEC,}));
        
        btnAppend = new JButton("+");
        panel_8.add(btnAppend, "1, 1");
        
        btnRemove = new JButton("-");
        panel_8.add(btnRemove, "1, 2");
        
        JPanel panel_6 = new JPanel();
        panel_1.add(panel_6, BorderLayout.SOUTH);
        panel_6.setLayout(new BorderLayout(0, 0));
        
        btnCommit = new JButton("Commit");
        panel_6.add(btnCommit, BorderLayout.EAST);
        
        btnRefresh = new JButton("Refresh");
        panel_6.add(btnRefresh, BorderLayout.CENTER);
        
        pnlBentos = new PnlBentos();
        tabbedPane.addTab("Bentos", null, pnlBentos, null);

    }

    public class Wrapper implements IWrapper<Component> {

        private IStoreManager stores;
        
        private ActionListener btnRefresh_Click= new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                stores.whereReset();
                if (chkName.isSelected()) {
                    stores.where(stores.getNameField()).contain(txtName.getText());                    
                }
                
                if (chkTel.isSelected()) {
                    stores.where(stores.getTelField()).contain(txtTel.getText());                    
                }
                
                stores.refresh();
                
                table.updateUI();
            }
        };
        
        private ActionListener btnCommit_Click= new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                stores.commit();
                stores.refresh(); // TODO: 
                table.updateUI();
            }
        };
        
        private ActionListener btnAppend_Click= new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                stores.add(stores.create().setName("").setTel(""));
                table.updateUI();
            }
        };
        
        private ActionListener btnRemove_Click= new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int index_select= table.getSelectedRow();
                if (index_select<0) return;
                if (index_select>= table.getRowCount()) return;
                
                stores.remove(index_select);
                table.updateUI();
            }
        };
        
        private AbstractTableModel model_stores= new AbstractTableModel(){
            private String[] columns= {"Name", "Tel"};
            
            @Override
            public String getColumnName(int column) {
                return columns[column];
            }

            @Override
            public int getRowCount() {
                return stores.size();
            }

            @Override
            public int getColumnCount() {
                return columns.length;
            }

            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                if (columnIndex==0) return stores.get(rowIndex).getName();
                if (columnIndex==1) return stores.get(rowIndex).getTel();
                return null;
            }

            @Override
            public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
                if (columnIndex==0) {
                    stores.get(rowIndex).setName(String.valueOf(aValue));
                    stores.alert(stores.get(rowIndex));
                }
                if (columnIndex==1) {
                    stores.get(rowIndex).setTel(String.valueOf(aValue));
                    stores.alert(stores.get(rowIndex));
                }
            }

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return true;
            }
            
            
        }; 
        
        private void initComponent() {
            table.setModel(model_stores);
            table.updateUI();
            
            btnRefresh.addActionListener(btnRefresh_Click);
            btnCommit.addActionListener(btnCommit_Click);
            btnAppend.addActionListener(btnAppend_Click);
            btnRemove.addActionListener(btnRemove_Click);
        }
        
        @Override
        public Component get() {
            return PnlStores.this;
        }
        
        public Wrapper(bento.api.IBentoClient bentoClient) {
            PnlStores.this.wrapper= this;
            stores = bentoClient.getStores();
            initComponent();
        }
    }
}
